<template>
  <div class="diymic-table-box">
    <el-table
      :data="dataSource"
      v-loading="loading"
      height="100%"
      style="width: 100%"
      empty-text="暂无数据"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
      :default-sort="defaultSort"
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <!-- 多选列 -->
      <el-table-column
        v-if="rowSelection"
        type="selection"
        width="55"
        :selectable="selectable"
        align="center"
      />
      <el-table-column type="index" width="50" label="序号"> </el-table-column>

      <!-- 动态列 -->
      <el-table-column
        show-overflow-tooltip
        v-for="col in visibleColumns"
        :key="col.prop"
        :prop="col.prop"
        :label="col.label"
        :width="col.width"
        :sortable="col.sortable || false"
        :align="col.align || 'center'"
      >
        <template v-if="$scopedSlots[col.prop]" v-slot="scope">
          <slot :name="col.prop" v-bind="scope" />
        </template>
      </el-table-column>

      <el-table-column label="操作" :width="optWidth">
        <template v-slot="scope">
          <slot name="operationBtn" v-bind="scope" />
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-area">
      <pagination
        :total="total"
        :page.sync="page.pageNum"
        :limit.sync="page.pageSize"
        @pagination="handlePaginationChange"
      />
    </div>
  </div>
</template>

<script>
export default {
  name: "DataTable",
  props: {
    columns: {
      type: Array,
      default: () => [],
    },
    dataSource: {
      type: Array,
      default: () => [],
    },
    loading: {
      type: Boolean,
      default: false,
    },
    page: {
      type: Object,
      default: () => ({
        pageNum: 1,
        pageSize: 10,
      }),
    },
    total: {
      type: Number,
      default: 0,
    },
    showPagination: {
      type: Boolean,
      default: true,
    },
    rowSelection: {
      type: Boolean,
      default: false,
    },
    defaultSort: {
      type: Object,
      default: () => ({}),
    },
    optWidth: {
      type: String,
      default: "160px",
    },
    //可选和不可选配置
    selectable: {
      type: Function,
      default: () => () => true,
    },
  },
  computed: {
    visibleColumns() {
      return this.columns.filter((col) => col.show !== false);
    },
  },
  methods: {
    handlePaginationChange() {
      this.$emit("getList");
    },
    handleSortChange({ prop, order }) {
      this.$emit("sort-change", { prop, order });
    },
    handleSelectionChange(selection) {
      this.$emit("selection-change", selection);
    },
  },
};
</script>

<style scoped lang="scss">
.diymic-table-box {
  display: flex;
  flex-direction: column;
  .el-table {
    height: calc(100% - 50px);
  }
  .pagination-area {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    height: 30px;
  }
}
</style>
